package com.ruoyi.crm.modules.quartz;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.crm.modules.sms.CrmSmsService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;
import java.util.Set;

import static com.ruoyi.crm.modules.quartz.RemindedService.JOB_GROUP_REMIND;


@Slf4j
public class RemindedJob extends QuartzJobBean {
    private Scheduler scheduler;
    private CrmSmsService crmSmsService;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        scheduler = SpringUtils.getBean(Scheduler.class);
        crmSmsService = SpringUtils.getBean(CrmSmsService.class);

        Trigger trigger = context.getTrigger();
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        // 将添加任务的时候存进去的数据拿出来
        String phone = (String) jobDataMap.get("phone");
        String message = (String) jobDataMap.get("message");
        // 编写任务的逻辑

        if (log.isDebugEnabled()) {
            log.debug("定时任务开始执行：{},{},{}", jobDetail.getKey(), trigger.getKey());
            log.debug("执行定时执行内容：{},{}", phone, message);
        }
        crmSmsService.sendMessage(phone, message, "1", new String[] {phone, message});
        try {
            if (log.isDebugEnabled()) {
                try {
                    Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupStartsWith(JOB_GROUP_REMIND));
                    log.debug("删除前：组{}共有{}个定时任务", JOB_GROUP_REMIND, jobKeys.size());
                    for (JobKey jobKey : jobKeys) {
                        List<? extends Trigger> triggersOfJob = scheduler.getTriggersOfJob(jobKey);
                        log.debug("job:{}, {}", jobKey.getGroup(), jobKey.getName());
                        for (Trigger tempTrigger : triggersOfJob) {
                            log.debug("status:{}", scheduler.getTriggerState(tempTrigger.getKey()));
                            if(tempTrigger instanceof CronTrigger) {
                                log.debug("cron:{}", ((CronTrigger)tempTrigger).getCronExpression());
                            }
                        }
                    }
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
            // 执行之后删除任务
            // 暂停触发器的计时
            scheduler.pauseTrigger(trigger.getKey());
            // 移除触发器中的任务
            scheduler.unscheduleJob(trigger.getKey());
            // 删除任务
            scheduler.deleteJob(jobDetail.getKey());
            if (log.isDebugEnabled()) {
                Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupStartsWith(JOB_GROUP_REMIND));
                log.debug("删除后：组{}共有{}个定时任务", JOB_GROUP_REMIND, jobKeys.size());
            }
        } catch (SchedulerException e) {
            log.error(e.getMessage(), e);
        }
    }
}
